// 引入 Mock
import Mock from 'mockjs'

// 定义数据类型
let { data } = Mock.mock({
  //500条数据
  'data|66': [
    {
      // 商品种类
      'tips|1': ['白银会员', '黄金会员', '铂金会员'],
      // 商品Id
      'id|+1': 1,
      //联系方式
      tel: /^1([38][0-9]|4[5-9]|5[0-3,5-9]|66|7[0-8]|9[89])[0-9]{8}$/,
      //密码
      password: /^[a-z0-9]+$/i,
      //卡号
      idCard: /^1([38][0-9]|4[5-9]|5[0-3,5-9]|66|7[0-8]|9[89])[0-9]{3}$/,
      //充值方式
      'fanshi|1': ['微信', '支付宝', '银行卡', '现金'],
      //充值套餐
      'taocan|1': ['未选择', '充1000送200', '充2000送500'],
      //zidingyi
      'zidingyi|1': ['200', '100', '300', '600', '500'],
    },
  ],
})
//获取数据和搜索接口
Mock.mock('/api/vip', 'post', (option) => {
  const { val, currentPage, pageSize } = JSON.parse(option.body)
  let result = []
  let count = 0
  //搜索
  if (val) {
    const filterValue = data.filter((item) => {
      return item.tel == val || item.idCard == val || item.tips == val
    })
    result = filterValue.slice((currentPage - 1) * pageSize, currentPage * pageSize)

    count = filterValue.length
  } else {
    //截取的数据
    result = data.slice((currentPage - 1) * pageSize, currentPage * pageSize)
    count = data.length
  }

  return {
    result: result,
    status: 0,
    //总数
    total: count,
  }
})

//删除的接口
Mock.mock('api/remove', 'post', (option) => {
  const { id } = JSON.parse(option.body)

  data = data.filter((item) => item.id !== id)

  return {
    status: 0,
    msg: '删除成功',
  }
})

//增加接口
Mock.mock('api/add', 'post', (option) => {
  const { state } = JSON.parse(option.body)
  // console.log(state)
  console.log(data)

  data = [...data, { ...state, id: data.length + 1 }]
  console.log(data)

  return {
    status: 0,
    msg: '新增成功',
  }
})

//编辑接口
Mock.mock('api/edit', 'post', (option) => {
  const { state } = JSON.parse(option.body)
  if (state) {
    data.forEach((item) => {
      if (item.id == state.id) {
        console.log(item)

        Object.assign(item, state)
        console.log(item)
      }
    })
  }
  return {
    status: 0,
    msg: '编辑成功',
  }
})

export default Mock
